package com.qdc.datacenters.controller;

import com.github.pagehelper.PageInfo;
import com.qdc.datacenters.annotation.Log;
import com.qdc.datacenters.domain.ResponseResult;
import com.qdc.datacenters.domain.query.rule.ChatMsgRuleAddQuery;
import com.qdc.datacenters.domain.query.rule.ChatMsgRuleQuery;
import com.qdc.datacenters.domain.query.rule.ChatMsgTimeoutListQuery;
import com.qdc.datacenters.domain.vo.rule.ChatMsgRuleListVo;
import com.qdc.datacenters.domain.vo.rule.ChatMsgRuleVo;
import com.qdc.datacenters.domain.vo.rule.GroupTimeoutListVo;
import com.qdc.datacenters.domain.vo.rule.SingleTimeoutListVo;
import com.qdc.datacenters.enums.BusinessTypeEnum;
import com.qdc.datacenters.service.IChatMsgRuleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author danmo
 * @date 2024-03-18 10:15
 **/
@Tag(name = "会话超时规则管理")
@RestController
@RequestMapping("/rule")
public class ChatMsgRuleController extends BaseController {

    @Autowired
    private IChatMsgRuleService iChatMsgRuleService;

    @Log(title = "新增规则", businessType = BusinessTypeEnum.INSERT)
    @PreAuthorize("@authz.hasPerm('rule:add')")
    @Operation(summary = "新增规则", method = "POST")
    @PostMapping("/add")
    public ResponseResult addQiRule(@RequestBody @Validated ChatMsgRuleAddQuery query) {
        iChatMsgRuleService.add(query);
        return ResponseResult.success();
    }

    @Log(title = "修改规则", businessType = BusinessTypeEnum.UPDATE)
    @PreAuthorize("@authz.hasPerm('rule:edit')")
    @Operation(summary = "修改规则", method = "POST")
    @PostMapping("/edit/{id}")
    public ResponseResult edit(@PathVariable("id") Long id, @RequestBody @Validated ChatMsgRuleAddQuery query) {
        query.setId(id);
        iChatMsgRuleService.edit(query);
        return ResponseResult.success();
    }

    @Log(title = "删除规则", businessType = BusinessTypeEnum.DELETE)
    @PreAuthorize("@authz.hasPerm('rule:delete')")
    @Operation(summary = "删除规则", method = "POST")
    @PostMapping("/delete")
    public ResponseResult delRule(@RequestBody ChatMsgRuleQuery query) {
        iChatMsgRuleService.delete(query);
        return ResponseResult.success();
    }

    @Log(title = "获取规则详情", businessType = BusinessTypeEnum.SELECT)
    @PreAuthorize("@authz.hasPerm('rule:get')")
    @Operation(summary = "获取规则详情", method = "POST")
    @PostMapping("/get/{id}")
    public ResponseResult<ChatMsgRuleVo> getRuleDetail(@PathVariable("id") Long id, @RequestBody ChatMsgRuleQuery query) {
        query.setId(id);
        ChatMsgRuleVo detail = iChatMsgRuleService.getRuleDetail(query);
        return ResponseResult.success(detail);
    }

    @Log(title = "获取规则列表", businessType = BusinessTypeEnum.SELECT)
    @PreAuthorize("@authz.hasPerm('rule:list')")
    @Operation(summary = "获取规则列表", method = "POST")
    @PostMapping("/list")
    public ResponseResult<PageInfo<ChatMsgRuleListVo>> getRuleList(@RequestBody ChatMsgRuleQuery query) {
        startPage(query.getPageIndex(), query.getPageSize(), query.getSortField(), query.getSort());
        return ResponseResult.success(iChatMsgRuleService.getRulePageList(query));
    }

    @Log(title = "获取单聊超时列表", businessType = BusinessTypeEnum.SELECT)
    @PreAuthorize("@authz.hasPerm('rule:single:list')")
    @Operation(summary = "获取单聊超时列表", method = "POST")
    @PostMapping("/single/list")
    public ResponseResult<PageInfo<SingleTimeoutListVo>> getSingleTimeOutList(@RequestBody ChatMsgTimeoutListQuery query) {
        startPage(query.getPageIndex(), query.getPageSize(), query.getSortField(), query.getSort());
        return ResponseResult.success(new PageInfo<>(iChatMsgRuleService.getSingleTimeOutList(query)));
    }

    @Log(title = "获取群聊超时列表", businessType = BusinessTypeEnum.SELECT)
    @PreAuthorize("@authz.hasPerm('rule:group:list')")
    @Operation(summary = "获取群聊超时列表", method = "POST")
    @PostMapping("/group/list")
    public ResponseResult<PageInfo<GroupTimeoutListVo>> getGroupTimeOutList(@RequestBody ChatMsgTimeoutListQuery query) {
        startPage(query.getPageIndex(), query.getPageSize(), query.getSortField(), query.getSort());
        return ResponseResult.success(new PageInfo<>(iChatMsgRuleService.getGroupTimeOutList(query)));
    }
}
